
/**
 * 深拷贝
 */
const deepCopy = (obj: Object) => {
  console.log('obj', obj)
  return JSON.parse(JSON.stringify(obj))
}

/**
 * 延迟执行
 */
const timeSleep = (time: number) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true)
    }, time)
  })
}

/**
 *
 */
/**
 * 写工具要注意思路，以后些什么东西先想好应该怎么写，这样会节省很多时间，思路也更清晰
 * 1000001.09 => 一百万零一 点 零九
 * 要素，分断所有分位，小数点只取第一位，随后的小数点不管，例如 10001.1.1 只读取 一万零一 点 一
 * 首先从上到下判断所有分位，也就是判断是否为 百万、十万、万、千 以此类推，不应设置上限，或者说上限为亿、或者万
 * 例如 100 0000 0000 应该读作 一百万万。这要看怎么设置最大进制
 */
const translateDigitToChinese = (num: number, digit: number = 0) => {
  if (isNaN(num)) {
    return 'Error: param num is not a number'
  }
  let numStr = String(num)
  let [maxNumStr, minNumStr] = numStr.split('.')
  const chineseBaseList = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']
  let chineseMinStr = ''
  if (minNumStr) {
    let minNumStrArr = minNumStr.split('')
    minNumStrArr.forEach((item: string, index: number) => {
      let itemNum = Number(item)
      chineseMinStr += chineseBaseList[itemNum]
    })
  }
  const unitList = ['千', '百', '十', '']
  let maxNumStrLen = maxNumStr.length
  if (Number(maxNumStr) == 0) {
    return chineseBaseList[0] + (chineseMinStr ? '点' + chineseMinStr : '')
  } else {
    let needZeroNumber = (4 - (maxNumStrLen % 4)) % 4
    for (let i = 1; i <= needZeroNumber; i++) {
      maxNumStr = '0' + maxNumStr
    }
    maxNumStrLen = maxNumStr.length
    let groupNumber = maxNumStrLen / 4
    let groupNumbeArr: string[][] = Array.from(Array(groupNumber), () => Array(4)) // 创建一个二维数组
    for (let i = 0; i < groupNumber; i ++) {
      groupNumbeArr[i][0] = maxNumStr[(i * 4) + 0]
      groupNumbeArr[i][1] = maxNumStr[(i * 4) + 1]
      groupNumbeArr[i][2] = maxNumStr[(i * 4) + 2]
      groupNumbeArr[i][3] = maxNumStr[(i * 4) + 3]
    }
    let chineseMaxStr = ''
    for (let row = 0; row < groupNumbeArr.length; row++) {
      for (let col = 0; col < 4; col++) {
        if (groupNumbeArr[row][col] !== '0') {
          chineseMaxStr += chineseBaseList[Number(groupNumbeArr[row][col])] + unitList[col]
        }
      }
      for (let addUnit = (groupNumber - row); addUnit > 1; addUnit--) {
        chineseMaxStr += '万'
      }
    }
    return chineseMaxStr + (chineseMinStr ? '点' + chineseMinStr : '')
  }
}

export {
  deepCopy,
  timeSleep,
  translateDigitToChinese,
}
